package cn.iocoder.yudao.module.reading.controller.app.recommendation;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.reading.controller.app.recommendation.vo.RecommendationZoneRespVO;
import cn.iocoder.yudao.module.reading.service.recommendation.RecommendationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 用户 APP - 推荐系统
 *
 * @author 芋道源码
 */
@Tag(name = "用户 APP - 推荐系统")
@RestController
@RequestMapping("/reading/recommendations")
@Validated
@Slf4j
public class RecommendationController {

    @Resource
    private RecommendationService recommendationService;

    @GetMapping("/zones")
    @Operation(summary = "获取推荐专区列表")
    @PreAuthenticated
    public CommonResult<List<RecommendationZoneRespVO>> getRecommendationZones(
            @Parameter(description = "语言类型", example = "en") @RequestParam(value = "lang", required = false) String lang,
            @Parameter(description = "专区类型", example = "featured") @RequestParam(value = "zoneType", required = false) String zoneType) {
        Long userId = getLoginUserId();
        List<RecommendationZoneRespVO> result = recommendationService.getRecommendationZones(userId, lang, zoneType);
        return success(result);
    }

    @GetMapping("/zones/{id}")
    @Operation(summary = "获取推荐专区详情")
    @PreAuthenticated
    public CommonResult<RecommendationZoneRespVO> getRecommendationZoneDetail(
            @Parameter(description = "专区ID", required = true, example = "1") @PathVariable("id") Long id,
            @Parameter(description = "内容限制数量", example = "10") @RequestParam(value = "limit", required = false) Integer limit) {
        Long userId = getLoginUserId();
        RecommendationZoneRespVO result = recommendationService.getRecommendationZoneDetail(userId, id, limit);
        return success(result);
    }

    @GetMapping("/zones/type/{type}")
    @Operation(summary = "根据类型获取推荐专区")
    @PreAuthenticated
    public CommonResult<List<RecommendationZoneRespVO>> getRecommendationZonesByType(
            @Parameter(description = "专区类型", required = true, example = "featured") @PathVariable("type") String type,
            @Parameter(description = "语言类型", example = "en") @RequestParam(value = "lang", required = false) String lang) {
        Long userId = getLoginUserId();
        List<RecommendationZoneRespVO> result = recommendationService.getRecommendationZonesByType(userId, type, lang);
        return success(result);
    }

    @GetMapping("/personalized")
    @Operation(summary = "获取个性化推荐")
    @PreAuthenticated
    public CommonResult<RecommendationZoneRespVO> getPersonalizedRecommendations(
            @Parameter(description = "语言类型", example = "en") @RequestParam(value = "lang", required = false) String lang,
            @Parameter(description = "限制数量", example = "10") @RequestParam(value = "limit", required = false) Integer limit) {
        Long userId = getLoginUserId();
        RecommendationZoneRespVO result = recommendationService.getPersonalizedRecommendations(userId, lang, limit);
        return success(result);
    }

    @GetMapping("/age/{targetAge}")
    @Operation(summary = "根据年龄段获取推荐")
    @PreAuthenticated
    public CommonResult<List<RecommendationZoneRespVO>> getRecommendationsByAge(
            @Parameter(description = "目标年龄段", required = true, example = "3-6岁") @PathVariable("targetAge") String targetAge,
            @Parameter(description = "语言类型", example = "en") @RequestParam(value = "lang", required = false) String lang) {
        Long userId = getLoginUserId();
        List<RecommendationZoneRespVO> result = recommendationService.getRecommendationsByAge(userId, targetAge, lang);
        return success(result);
    }

}
